//
//  GPSOSMessageCell.m
//  Keping
//
//  Created by 23 on 2017/12/9.
//  Copyright © 2017年 柯平. All rights reserved.
//

#import "GPSOSMessageCell.h"

@interface GPSOSMessageCell ()

/**titleLabel*/
@property(nonatomic,weak) UILabel *sosTitleLabel;
/**locationLabel*/
@property(nonatomic,weak) UILabel *sosLocationLabel;
/**蹄片*/
@property(nonatomic,weak) UIImageView *sosImageView;


@end

@implementation GPSOSMessageCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
       
        UILabel *sosTitleLabel = [[UILabel alloc] init];
        sosTitleLabel.text = @"Emergency information";
        [self.contentBackground addSubview:sosTitleLabel];
        self.sosTitleLabel = sosTitleLabel;
        
        UILabel *sosLocationLabel = [[UILabel alloc] init];
        sosLocationLabel.text = @"Location: GuangZhou Tianhe road no.111";
        sosLocationLabel.numberOfLines = 0;
        sosLocationLabel.preferredMaxLayoutWidth = 200 - 10 - 10 - 60 -10;
        [self.contentBackground addSubview:sosLocationLabel];
        self.sosLocationLabel = sosLocationLabel;
        
        UIImageView *sosImageView = [[UIImageView alloc] init];
        [self.contentBackground addSubview:sosImageView];
        self.sosImageView = sosImageView;
        
        [sosTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.mas_equalTo(self.contentBackground).offset(10);
        }];
        
        [sosImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(sosTitleLabel.mas_bottom).offset(10);
            make.right.mas_equalTo(self.contentBackground.mas_right).offset(-10);
            make.width.height.mas_equalTo(60);
            make.bottom.mas_equalTo(self.contentBackground.mas_bottom).offset(-10);
        }];
        
        [sosLocationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(sosTitleLabel);
            make.top.mas_equalTo(sosImageView);
            make.right.mas_equalTo(sosImageView.mas_left).offset(-10);
            make.bottom.mas_equalTo(sosImageView.mas_bottom);
        }];
        
    }
    return self;
}

- (void)setMessage:(GPMessageItem *)message
{
    //设置父类的值和约束
    [super setMessage:message];
    
    [self.contentBackground mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(200);
    }];
    
    self.contentBackground.backgroundColor = [UIColor whiteColor];
    
    //赋值
    self.sosTitleLabel.text = @"Emergency information";
    if (!message.isSender) {
        [self.sosImageView sd_setImageWithURL:[NSURL URLWithString:message.fileURLPath] placeholderImage:nil];
    }else{
        [self.sosImageView sd_setImageWithURL:nil placeholderImage:message.image];
    }
    
    self.sosLocationLabel.text = message.address;
    
    
    if( CGRectGetMaxY(self.sosLocationLabel.frame) > CGRectGetMaxY(self.sosImageView.frame)){
    
        [self.sosLocationLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(self.contentBackground.mas_bottom).offset(-10);
        }];
        
    }else{
        
        [self.sosImageView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(self.contentBackground.mas_bottom).offset(-10);
        }];
        
    }
    

    
    //更新
    [self layoutIfNeeded];
    
    //计算cell的高度
    CGFloat cellHeight = CGRectGetMaxY(self.contentBackground.frame) + 10;
    message.cellHeight = cellHeight;
    
}



@end
